define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () 
        {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'depot/storage/index',
                    add_url: 'depot/storage/add',
                    edit_url: 'depot/storage/edit',
                    del_url: 'depot/storage/del',
                    info_url: 'depot/storage/info',
                    agree_url: 'depot/storage/agree',
                    reject_url: 'depot/storage/reject',
                    revoke_url: 'depot/storage/revoke',
                    confirm_url: 'depot/storage/confirm',
                    table: 'depot_storage',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                onLoadSuccess:function()
                {
                    $('.btn-editone').data('area',['100%','100%'])
                    $(".btn-add").data("area", ["100%", "100%"]);
                    $(".btn-edit").data('area', ['100%', '100%'])
                },
                columns: [
                    [
                        { field: 'id', title: __('Id'), operate:false },
                        { field: 'code', title: __('Code'), operate: 'LIKE' },
                        { field: 'supplier.name', title: __('supplier') },
                        { field: 'type', title: __('Type'), searchList: { "1": __('直销入库'), "2": __('退货入库') }, formatter: Table.api.formatter.normal },
                        { field: 'amount', title: __('StorageAmount'), operate: 'BETWEEN', sortable:true },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', sortable:true, addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'status', title: __('Status'), searchList: Config.statuslist, formatter: Table.api.formatter.status },
                        { 
                            field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'info',
                                    title: '入库详情',
                                    extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                                    classname: "btn btn-xs btn-success btn-dialog",
                                    icon: 'fa fa-eye',
                                    url: $.fn.bootstrapTable.defaults.extend.info_url,
                                },
                                {
                                    name:'agree',
                                    title:'通过审核',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    extend: 'data-toggle=\"tooltip\"data-container=\"body\"',
                                    icon:'fa fa-leaf',
                                    confirm:'确认通过审核吗？',
                                    url: $.fn.bootstrapTable.defaults.extend.agree_url,
                                    success: data => $(".btn-refresh").trigger("click"),
                                    visible: row => row.status == 0 ? true : false,
                                },
                                {
                                    name:'reject',
                                    title:'拒绝审核',
                                    classname:'btn btn-xs btn-info btn-ajax',
                                    icon:'fa fa-exclamation-triangle',
                                    confirm:'确认拒绝审核吗？',
                                    url:$.fn.bootstrapTable.defaults.extend.reject_url,
                                    extend: 'data-toggle=\"tooltip\" data-container=\"body\"',
                                    success: data => $(".btn-refresh").trigger("click"),
                                    visible: row => row.status == 0 ? true : false
                                },
                                {
                                    name:'revoke',
                                    title:'撤销审核',
                                    classname:'btn btn-xs btn-danger btn-ajax',
                                    extend: 'data-toggle=\"tooltip\" data-container=\"body\"',
                                    icon:'fa fa-reply',
                                    url:$.fn.bootstrapTable.defaults.extend.revoke_url,
                                    confirm:'确认要撤回审核吗？',
                                    success: data => $(".btn-refresh").trigger("click"),
                                    visible: row => (row.status == '1' || row.status == '2') ? true : false,
                                },
                                {
                                    name:'confirm',
                                    title:'确认入库',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    extend: 'data-toggle="tooltip" data-container="body"',
                                    icon:'fa fa-leaf',
                                    confirm:'确认确认入库吗？',
                                    url:$.fn.bootstrapTable.defaults.extend.confirm_url,
                                    success: data => $(".btn-refresh").trigger("click"),
                                    visible: row => row.status == 2 ? true : false
                                },
                                {
                                    name:'edit',
                                    title:'编辑',
                                    classname:'btn btn-xs btn-success btn-editone',
                                    extend: 'data-toggle=\"tooltip\" data-container=\"body\"',
                                    icon:'fa fa-pencil',
                                    url: $.fn.bootstrapTable.defaults.extend.edit_url,
                                    visible:row => row.status == 3 ? false : true
                                },
                            ]
                        }
                    ]
                ]
            });


            $('.btn-agree').on('click', function () {
                var ids = Table.api.selectedids(table);
                
                layer.confirm('确定要审核通过吗?', { title: '审核通过', btn: ['是', '否'] },function (index) {
                    $.post($.fn.bootstrapTable.defaults.extend.agree_url, {ids: ids}, function (response) {
                        layer.close(index)

                        if(response.code) 
                        {
                            Toastr.success(response.msg)
                            $(".btn-refresh").trigger('click');
                        }else
                        {
                            Toastr.error(response.msg)
                        }
                    }, 'json')
                })
            });

            $('.btn-reject').on('click', function () {
                var ids = Table.api.selectedids(table);
                
                layer.confirm('确定要拒绝审核吗?', { title: '拒绝审核', btn: ['是', '否'] },function (index) {
                    $.post($.fn.bootstrapTable.defaults.extend.reject_url, {ids: ids}, function (response) {
                        layer.close(index)

                        if(response.code) 
                        {
                            Toastr.success(response.msg)
                            $(".btn-refresh").trigger('click');
                        }else
                        {
                            Toastr.error(response.msg)
                        }
                    }, 'json')
                })
            });

            $('.btn-revoke').on('click', function () {
                var ids = Table.api.selectedids(table);
                
                layer.confirm('确定要撤销审核吗?', { title: '撤销审核', btn: ['是', '否'] },function (index) {
                    $.post($.fn.bootstrapTable.defaults.extend.revoke_url, {ids: ids}, function (response) {
                        layer.close(index)

                        if(response.code) 
                        {
                            Toastr.success(response.msg)
                            $(".btn-refresh").trigger('click');
                        }else
                        {
                            Toastr.error(response.msg)
                        }
                    }, 'json')
                })
            });

            $('.btn-confirm').on('click', function () {
                var ids = Table.api.selectedids(table);
                
                layer.confirm('确定要入库吗?', { title: '确认入库', btn: ['是', '否'] },function (index) {
                    $.post($.fn.bootstrapTable.defaults.extend.confirm_url, {ids: ids}, function (response) {
                        layer.close(index)

                        if(response.code) 
                        {
                            Toastr.success(response.msg)
                            $(".btn-refresh").trigger('click');
                        }else
                        {
                            Toastr.error(response.msg)
                        }
                    }, 'json')
                })
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function ()
        {
            //选择供应商
            $('#ModelSupplier').on('show.bs.modal', function (e) {
                Controller.api.GetSupplier();
            });

            $('#ModelSupplier').on('hidden.bs.modal', function () {
                $('#TableSupplier').bootstrapTable('destroy');
            });

            //选择入库商品
            $('#ModelProduct').on('show.bs.modal', function (e) {
                Controller.api.GetProduct();
            });

            $('#ModelProduct').on('hidden.bs.modal', function () {
                $('#TableProduct').bootstrapTable('destroy');
            });
        
            Controller.api.bindevent();
        },
        edit: function ()
        {
            //选择供应商
            $('#ModelSupplier').on('show.bs.modal', function (e) {
                Controller.api.GetSupplier();
            });

            $('#ModelSupplier').on('hidden.bs.modal', function () {
                $('#TableSupplier').bootstrapTable('destroy');
            });

            //选择入库商品
            $('#ModelProduct').on('show.bs.modal', function (e) {
                Controller.api.GetProduct();
            });

            $('#ModelProduct').on('hidden.bs.modal', function () {
                $('#TableProduct').bootstrapTable('destroy');
            });
        
            Controller.api.bindevent();
        },
        del: function()
        {
            Controller.api.bindevent();
        },
        info: function()
        {
            Controller.api.bindevent();
        },
        agree: function()
        {
            Controller.api.bindevent();
        },
        reject: function()
        {
            Controller.api.bindevent();
        },
        revoke: function()
        {
            Controller.api.bindevent();
        },
        confirm: function()
        {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () 
            {
                Form.api.bindevent($("form[role=form]"));
            },
            //供应商
            GetSupplier: function()
            {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                    index_url: 'depot/async/supplier/index',
                    select_url: 'depot/async/storage/supplier',
                    table: 'supplier',
                    }
                });
        
                var table = $("#TableSupplier");
        
                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    sortName: 'id',
                    toolbar: '#ToolbarSupplier',
                    search: false,
                    columns: [
                    [
                        { field: 'id', title: __('ID'), sortable: true, operate: false},
                        { field: 'name', title: __('SupplierName'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'region_text', title: __('Area'), operate: false },
                        {
                            field: 'operate',
                            title: __('Operate'), 
                            table: table, 
                            events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate, 
                            buttons: [
                                {
                                    name: 'info', 
                                    text: '选择', 
                                    title: '选择', 
                                    icon: 'fa fa-magic', 
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax', 
                                    extend: 'data-toggle=\'tooltip\'',
                                    url: $.fn.bootstrapTable.defaults.extend.select_url, 
                                    success: function (data, ret) 
                                    {
                                        if(data)
                                        {
                                            $("#ShowSupplier").val(data.name);
                                            $("#supplierid").val(data.id);
                                            $('#mobile').val(data.mobile);
                                            $('#region').val(data.region_text);
                                        }

                                        $('#ModelSupplier').modal('hide');
                                    }
                                }
                            ]
                        }
                    ]
                    ],
                });
        
                // 为表格绑定事件
                Table.api.bindevent(table);
            },
            //入库商品
            GetProduct: function ()
            {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                    index_url: 'depot/async/product/index',
                    select_url: 'depot/async/storage/product',
                    table: 'product',
                    }
                });
        
                var table = $("#TableProduct");
        
                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    sortName: 'id',
                    toolbar: '#ToolbarProduct',
                    search: false,
                    columns: [
                    [
                        { field: 'id', title: __('Id'), sortable: true, operate: false },
                        { field: 'name', title: __('ProductName'), operate: 'LIKE' },
                        { field: 'unit.name', title: __('ProductUnit') },
                        {
                        field: 'operate',
                        title: __('Operate'),
                        table: table,
                        events: Table.api.events.operate,
                        formatter: Table.api.formatter.operate,
                        buttons: [
                            {
                            name: 'info',
                            text: '选取',
                            title: '选取',
                            icon: 'fa fa-magic',
                            classname: 'btn btn-sm btn-success btn-magic btn-ajax',
                            url: $.fn.bootstrapTable.defaults.extend.select_url,
                            extend: 'data-toggle=\'tooltip\'',
                            success: function (data, ret) 
                            {
                                Product.add(data)
                            }
                            }
                        ]
                        }
                    ]
                    ]
                });
        
                // 为表格绑定事件
                Table.api.bindevent(table);
            },
        }
    };

    return Controller;
});


var Product = {
    //添加入库商品
    add: (row) => {
      $('#ModelProduct').modal('hide');
  
      if(JSON.stringify(row) == "{}")
      {
        return false
      }
  
      //不存在就追加
      var check = $(`#TbodyProduct tr[data-proid=${row.id}]`)
  
      if (check.length <= 0)
      {
        var str = `
          <tr data-proid='${row.id}'>
            <td>${row.id}<input type="hidden" name="prolist[]" value="${row.id}" required></td>
            <td>${row.name}</td>
            <td>${row.stock}</td>
            <td><input class='text-center' type="number" min="1" name="nums[]" value='1' onchange="Product.edit(this)" required placeholder='请输入商品数量' data-rule="required;digits"></td>
            <td><input class='text-center' type="number" min="1" name="price[]" value='' onchange="Product.price(this)" required placeholder="请输入商品单价" data-rule="required"></td>
            <td><input class='text-center' type="number" min="1" name="total[]" value='' onchange="Product.total(this)" required readonly placeholder="请输入商品合计" data-rule="required"></td>
            <td>
              <a class="btn btn-danger" onclick="Product.del(this)" data-toggle="tooltip" data-original-title="删除">
                <i class="fa fa-trash"></i>
              </a>
            </td>
            
          </tr>
        `;
        
        $("#TbodyProduct").append(str);
      }
    },
    //更新入库商品
    edit: (that) => {
      var relationnum = parseInt($(that).val());
  
      if (relationnum <= 0) {
        $(that).val(1)
        layer.msg('商品数量必须大于0', {
          time: 500,
        })
        return false
      }
  
      //更新商品总计
      Product.price(that)
    },
    //更新商品价格总计
    price: (that = false) => {
      var index = parseInt($(that).parent().parent().index())
      var index = isNaN(index) ? 0 : index
  
      if(isNaN(index))
      {
        layer.msg('商品单价、数量格式错误', {
          time: 500,
        })
        return false
      }
  
      var price = parseFloat($("input[name='price[]']").eq(index).val())
          price = isNaN(price) ? 0 : price.toFixed(2)
  
      if(isNaN(price) && price < 0)
      {
        layer.msg('请输入正确的价格格式', {
          time: 500,
        })
        return false
      }
  
      var nums = parseInt($("input[name='nums[]']").eq(index).val())
          nums = isNaN(nums) ? 1 : nums
  
      if(nums <= 0)
      {
        layer.msg('请输入商品数量', {
          time: 500,
        })
        return false
      }
  
      //判断元素是否存在
      var element = $("input[name='total[]']").eq(index)
      if(!element)
      {
        return false
      }
  
  
      // 总价
      var total = parseFloat(price*nums).toFixed(2)
      if(total >= 0)
      {
        //更新商品总价
        element.val(total)
  
        //手动触发change
        element.change()
      }
    },
    //更新订单价格
    total: (that = false) => {
      if(that)
      {
        var price = parseInt($(that).val())
  
        if(isNaN(price) && price < 0)
        {
          layer.msg('请输入正确的价格格式', {
            time: 500,
          })
          return false
        }
      }
  
      // 总价
      var total = 0
  
      //是否有不正确的格式数据
      var invalid = 0
  
      if($("input[name='total[]']").length > 0)
      {
        $("input[name='total[]']").each(function(){
          var amount = parseFloat($(this).val())
      
          if(isNaN(amount))
          {
            invalid++
          }
      
          if(!isNaN(amount) && amount > 0)
          {
            total += amount
          }
        })
      }else
      {
        total = 0
      }
  
      if(invalid > 0)
      {
        layer.msg(`有${invalid}个输入的商品总计格式不正确`, {
          time: 500,
        })
        return false
      }
  
      if(total >= 0)
      {
        total = total.toFixed(2)
        $("#amount").val(total)
  
        layer.msg('更新总价成功', {
          time: 500,
        })
      }
    },
    //删除入库商品
    del: (that = false) => {
      if(that)
      {
        $(that).parent().parent().remove()
        Product.total()
      }
    },
}